package com.three.productList.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.three.core.entity.Product;
import com.three.core.entity.User;
import com.three.productList.dao.ProductTypeDao;
import com.three.core.entity.ProductType;
import com.three.productList.feign.UserMyFeign;
import com.three.productList.service.ProductTypeService;

import com.three.core.vo.R;
import com.three.productList.vo.SearchForm;
import com.three.productList.vo.SearchForm2;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;


/**
 * @author ：li
 * @date ：Created in 2025/3/11 011 16:42
 * @description：
 * @modified By：
 * @version:
 */
@Service
public class ProductTypeServiceImpl implements ProductTypeService {
    @Autowired
    private ProductTypeDao productTypeDao;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private UserMyFeign userMyFeign;
    //查询所有商品类型
    public R<List<ProductType>> selectAllProductType() {
        List<ProductType> productTypes = productTypeDao.selectList(null);
        return new R(200, "查询成功",productTypes);
    }
    //分页查询表单数据
    @Override
    public R<Page<ProductType>> selectProductTypes(SearchForm2 searchForm) {
        QueryWrapper<ProductType> eq = new QueryWrapper<>();
        if(searchForm.getName() != null){
            eq.like("name",searchForm.getName());
        }
        Page<ProductType> page = new Page<>(searchForm.getCurrentPage(),searchForm.getPageSize());
        Page<ProductType> p = productTypeDao.selectPage(page, eq);
        return new R<Page<ProductType>>(200,"查询成功",p);
    }
    //根据id删除商品类型
    @Override
    public R<ProductType> deleteProductTypeById(Integer commentId) {
        int i = productTypeDao.deleteByProductId(commentId);
        if(i == 0){
            return new R<ProductType>(400,"删除失败",null);
        }
        return new R<ProductType>(200,"删除成功",null);
    }
    //是否允许被修改
    @Override
    public R<ProductType> updateIseditByChangeId(Integer commentId, Long isedit) {
        int update = productTypeDao.updateStatus(commentId,isedit);
        if(update == 0){
            return new R<ProductType>(400,"修改失败",null);
        }
        return new R<ProductType>(200,"修改成功",null);
    }

    //添加按钮
    @Override
    public R<ProductType> addProductType(ProductType productType) {
        //获取登录者的信息
        String o = (String) redisTemplate.opsForHash().get("user::login::" + request.getHeader("token"), "name");
        R<User> userR = userMyFeign.selectByName(o);
        productType.setCreateBy(userR.getData().getUserId());
        productType.setUpdateBy(userR.getData().getUserId());
        productType.setCreateTime(LocalDateTime.now());
        productType.setUpdateTime(LocalDateTime.now());
        int insert = productTypeDao.insert(productType);
        if(insert == 0){
            return new R<ProductType>(400,"添加失败",null);
        }
        return new R<ProductType>(200,"添加成功",null);
    }
    //确认修改
    @Override
    public R<ProductType> updateProductType(ProductType productType) {
        //获取登录者的信息
        String o = (String) redisTemplate.opsForHash().get("user::login::" + request.getHeader("token"), "name");
        R<User> userR = userMyFeign.selectByName(o);
        productType.setUpdateBy(userR.getData().getUserId());
        productType.setUpdateTime(LocalDateTime.now());
        int row = productTypeDao.updateById(productType);
        if(row == 0){
            return new R<ProductType>(400,"修改失败",null);
        }
        return new R<ProductType>(200,"修改成功",null);
    }


}
